//
//  ClassVideoTableViewCell.m
//  Products_B
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "ClassVideoTableViewCell.h"
#define kFont [UIFont systemFontOfSize:14]
@interface ClassVideoTableViewCell() {
   
    UIImageView *_headerImageView;
    UILabel *_nickLabel;
    UILabel *_contentLabel;
    UILabel *_timeLabel;
}

@end

@implementation ClassVideoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 30, 30)];
        _headerImageView.layer.cornerRadius = 15;
        _headerImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_headerImageView];
        
        CGFloat x = _headerImageView.frame.origin.x + _headerImageView.frame.size.width + 10;
        _nickLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 10, kWidth - 100, 20)];
        _nickLabel.font = kFont;
        [self.contentView addSubview:_nickLabel];
        
        // 该laebel需要动态返回高度
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth - 100 - 50, 20)];
        _contentLabel.font = kFont;
        _contentLabel.numberOfLines = 0;
        _contentLabel.alpha = 0.5;
        [self.contentView addSubview:_contentLabel];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 90, 20, 90, 20)];
        _timeLabel.font = kFont;
        _timeLabel.alpha = 0.5;
        [self.contentView addSubview:_timeLabel];
    }
    return self;
}

// 重写set方法,给cell的子控件赋值
- (void)setClassVideoModel:(ClassVideoModel *)classVideoModel {
    _classVideoModel = classVideoModel;
    [_headerImageView sd_setImageWithURL:[NSURL URLWithString:classVideoModel.head_img]];
    _nickLabel.text = classVideoModel.nick;
    _contentLabel.text = classVideoModel.content;
    _timeLabel.text = classVideoModel.create_time;
    
    [self layoutCellSubViews];
}

// 动态布局
- (void)layoutCellSubViews {
    CGFloat textH = [ClassVideoTableViewCell textForHeightWithTextString:_contentLabel.text font:kFont];
    _contentLabel.frame = CGRectMake(_headerImageView.frame.origin.x + _headerImageView.frame.size.width + 10, 30, kWidth - 100 - 50, textH);
    
    
}

+ (CGFloat)cellForHeigh:(ClassVideoModel *)model {
    CGFloat H = [ClassVideoTableViewCell textForHeightWithTextString:model.content font:kFont];
    return 10 + 20 + H + 30;
}

// 获取文字的高度
+ (CGFloat)textForHeightWithTextString:(NSString *)textString font:(UIFont *)font {
    NSDictionary *dic = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    CGRect bounds = [textString boundingRectWithSize:CGSizeMake(kWidth - 100 - 50, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return bounds.size.height;
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
